package cen.tms.ggtour;

import cen.tms.ggtour.data.DataManager;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class Events extends ListActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.events_layout);
		//Jonathan Code
		setListAdapter(getAdapter());
		startManagingCursor(((CursorAdapter) this.getListAdapter()).getCursor());
		this.getListView().setCacheColorHint(0x0021A5);
		//end Jonathan Code
		
		TextView title = (TextView) findViewById(android.R.id.title);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/book_antiqua.ttf");
        title.setTypeface(tf);
        title.setText("Events");
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		//Jonathan Code
		//store _id of Event into bundle
		Bundle thisEvent = new Bundle();
		thisEvent.putInt("_id", ((Cursor) l.getAdapter().getItem(position)).getInt(0));
		//place bundle into Intent
		Intent displayEvent = new Intent("cen.tms.ggtour.DISPLAY_EVENT");
		displayEvent.putExtras(thisEvent);
		//start display Activity
		startActivity(displayEvent);
		//end Jonathan Code
	}

	private CursorAdapter getAdapter(){
		return new CursorAdapter(this, DataManager.getInstance(this).getEvents(null, null, null, null), true) {

			@Override
			public View newView(Context context, Cursor cursor, ViewGroup parent) {
				LayoutInflater viewInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View convertView = viewInflater.inflate(R.layout.listevent, parent, false);
				return convertView;
			}

			@Override
			public void bindView(View view, Context context, Cursor cursor) {
				TextView txtName = (TextView) view.findViewById(R.id.listEventName);
				TextView txtDate = (TextView) view.findViewById(R.id.listEventDate);
				if (txtName != null) {
					txtName.setText(DataManager.getName(cursor));                            
				}
				if(txtDate != null) {
					txtDate.setText(DataManager.getDate(cursor));     
				}				
			}
		};
	}

	public void onTitleBackClick(View view) {
		this.onBackPressed();
	}
}
